package com.jsmtr.rms.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.entity.vo.ResumeInfo;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IDoSZService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.DateTools;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/Do/SZResume")
public class DoSZResumeController {

    @Resource
    private IUtilService utilService;
    @Resource
    private IDoSZService szService;
    @Resource
    private IDoResumeService doResumeService;
    @Resource
    private IProjectService projectService;

    //[start] 简历筛选主界面

    @RequestMapping("/")
    public String Index(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            String pid = request.getParameter("pid").trim();//招聘方案
            String fid = request.getParameter("fid").trim();//招聘流程
            String deptcode = request.getParameter("deptcode").trim();//责任部门

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            //名族
            model.addAttribute("minzu", utilService.optionList("B", "0"));
            //婚否
            model.addAttribute("hunfou", utilService.optionList("D", "0"));
            //政治面貌
            model.addAttribute("zzmm", utilService.optionList("E", "0"));
            //最高学历
            model.addAttribute("education", utilService.optionList("F", "0"));
            //专业技术职务类型
            model.addAttribute("posttype", utilService.optionList("I", "0"));
            //职业资格证
            model.addAttribute("jndj", utilService.optionList("O", "0"));
            /*//招聘岗位
            List<ProjectInfoJobs> jobs = projectService.getjobsbypid(pid);
            StringBuilder dstr = new StringBuilder();
            for (ProjectInfoJobs job : jobs) {
                dstr.append("<option value='" + job.getJobname() + "'>" + job.getJobname() + "</option>");
            }
            model.addAttribute("jobs", dstr);*/

            //招聘岗位
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);
            ProjectInfoFlow projectInfoFlow= projectService.getFolwById(fid);
            if(projectInfoFlow!=null){
                model.addAttribute("nowflow",projectInfoFlow.getNickname());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Resume/index";
    }

    //简历筛选-查询简历数据
    @SuppressWarnings("unchecked")
 /*   @RequestMapping("/getresumes")
    public void getresumes(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("workyearfrom", request.getParameter("workyearfrom").trim());
            _map.put("workyearto", request.getParameter("workyearto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());
            _map.put("rcount",request.getParameter("rcount").trim());

            Map<String, Object> map = szService.getResumesMap(page, perpage, resume, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
               *//* Object object = map.get("list");
                for (DUserResume dur : (List<DUserResume>) object) {
                    result.append("<tr><td><input type='checkbox' id='cb_" + dur.getId() + "' /></td>");
                    //result.append("<td>" + dur.getCode() + "</td>");
                    result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/Do/SZResume/View?id=" + dur.getId() + "'>" + dur.getName() + "</a>&nbsp;<span class='layui-badge'>4</span></td>");
                    result.append("<td>" + dur.getSexname() + "</td>");
                    result.append("<td>" + dur.getCardno() + "</td>");
                    result.append("<td>" + DateTools.BirthdayToAge(dur.getBirthday()) + "</td>");
                    result.append("<td>" + dur.getWorkexperience() + "年</td>");
                    result.append("<td>" + dur.getJname() + "</td>");
                    result.append("<td>" + dur.getMajorname() + "</td>");
                    result.append("<td>" + dur.getEducationname() + "</td>");
                    result.append("<td>" + dur.getTel() + "</td>");
                    result.append("<td>" + dur.getState() + "</td>");
                    result.append("<td>" + (dur.getSxbz() == null ? "" : dur.getSxbz()) + "</td>");
                    result.append("<td>" + (dur.getSxbz() == null ? "" : dur.getSxbz()) + "</td>");
                    if (dur.getOp1() == null || dur.getOp1().equals("未公布")) {
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    if (dur.getFqstate().equals("已放弃")
                            && dur.getFqfid().equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (dur.getNrid() != null) {
                            result.append("<td class='ytj'><span title='" + dur.getNjname() + "'>已推荐</span></td></tr>");
                        } else {
                            if (dur.getState().equals("未处理")) {
                                result.append("<td class='true'><span>未完成</span></td></tr>");
                            } else if (dur.getState().equals("未通过")) {
                                result.append("<td class='false'><span>已完成</span></td></tr>");
                            } else {
                                boolean bl = dur.getFid().equals(flowid);
                                result.append("<td class='" + bl + "'><span>" + (bl == true ? "未完成" : "已完成") + "</span></td></tr>");
                            }
                        }
                    }
                }*//*
                List<HashMap> object = (List<HashMap>)map.get("list");
                for (HashMap<String,Object> _user :  object) {
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user.get("ID") + "' /></td>");
                    //result.append("<td>" + dur.getCode() + "</td>");
                    Integer cardcount= Integer.parseInt(_user.get("CARDCOUNT").toString());
                    if (cardcount>1){
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + _user.get("ID") + "'>" + _user.get("NAME") + "</a>&nbsp;<span class='layui-badge'>"+cardcount+"</span></td>");
                    }else{
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + _user.get("ID") + "'>" + _user.get("NAME") + "</a></td>");
                    }
                    result.append("<td>" + _user.get("CARDNO") + "</td>");
                    result.append("<td>" + _user.get("SEXNAME") + "</td>");
                    result.append("<td>" + DateTools.BirthdayToAge((Date) _user.get("BIRTHDAY")) + "</td>");
                   *//* result.append("<td>" + _user.get("WORKEXPERIENCE") + "年</td>");*//*
                    result.append("<td>" + (_user.get("WORKEXPERIENCE") == null ? "" :_user.get("WORKEXPERIENCE")) + "</td>");
                    result.append("<td>" + _user.get("JNAME") + "</td>");
//                    result.append("<td>" + _user.get("MAJORNAME") + "</td>");
                    result.append("<td>" + (_user.get("MAJORNAME") == null ? "" :_user.get("MAJORNAME")) + "</td>");
//                    result.append("<td>" +  _user.get("EDUCATIONNAME") + "</td>");
                    result.append("<td>" + (_user.get("EDUCATIONNAME") == null ? "" :_user.get("EDUCATIONNAME")) + "</td>");
                    result.append("<td>" + _user.get("TEL") + "</td>");
                    result.append("<td>" + _user.get("STATE")  + "</td>");
                    result.append("<td>" + (_user.get("SXBZ") == null ? "" : _user.get("SXBZ")) + "</td>");

                    if (_user.get("OP1") == null || _user.get("OP1").equals("未公布")) {
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+ (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+"最近登录："+ (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString())+"<br/>个人情况：" + (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    if (_user.get("FQSTATE").equals("已放弃")
                            && _user.get("FQFID").equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user.get("NRID") != null) {
                            result.append("<td class='ytj'><span title='" + _user.get("NJNAME") + "'>已推荐</span></td></tr>");
                        } else {
                            if (_user.get("STATE").equals("未处理")) {
                                result.append("<td class='true'><span>未完成</span></td></tr>");
                            } else if (_user.get("STATE").equals("未通过")||_user.get("STATE").equals("不通过")||_user.get("STATE").equals("不合格")) {
                                result.append("<td class='false'><span>已完成</span></td></tr>");
                            } else {
                                boolean bl = _user.get("FID").equals(flowid);
                                result.append("<td class='" + bl + "'><span>" + (bl == true ? "未完成" : "已完成") + "</span></td></tr>");
                            }
                        }
                    }
                }
            } else {
                result.append("<tr><td colspan='16'>无符合条件的信息</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='16'>系统出错了</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }
    }*/
    //[end]

    @ResponseBody
    @RequestMapping(value = "/getSzResumeListLayuiJson", method = RequestMethod.GET)
    public LayuiDataTableResultBean getSzResumeListLayuiJson(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        try {
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            String flowid = request.getParameter("flowid").trim();

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("workyearfrom", request.getParameter("workyearfrom").trim());
            _map.put("workyearto", request.getParameter("workyearto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());
            _map.put("rcount",request.getParameter("rcount").trim());
            _map.put("mcontent",request.getParameter("mcontent"));

            Map<String, Object> map = szService.getResumesMap(page, limit, resume, _map);

            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            JSONArray jsonArray = new JSONArray();
            int num = 1;
            if (pageCount != 0) {
                List<HashMap> object = (List<HashMap>)map.get("list");
                for (HashMap<String,Object> _user :  object) {
                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("id_cb","cb_" + _user.get("ID"));
                    jsonObject.put("id",limit * (page - 1) + num);
                    jsonObject.put("cardcount", Integer.parseInt(_user.get("CARDCOUNT").toString())); //投简历总次数
                    jsonObject.put("ContextPath",request.getContextPath()+"/UserResume/ViewDResume?id=" + _user.get("ID"));
                    jsonObject.put("NAME", _user.get("NAME"));
                    jsonObject.put("CARDNO", _user.get("CARDNO"));
                    jsonObject.put("SEXNAME", _user.get("SEXNAME"));
                    jsonObject.put("BIRTHDAY", _user.get("BIRTHDAY"));
                    jsonObject.put("WORKEXPERIENCE", (_user.get("WORKEXPERIENCE")== null ? "" : (_user.get("WORKEXPERIENCE")+"年")));
                    jsonObject.put("JNAME", _user.get("JNAME"));
                    jsonObject.put("MAJORNAME", (_user.get("MAJORNAME")== null ? "" : _user.get("MAJORNAME")));
                    jsonObject.put("EDUCATIONNAME", (_user.get("EDUCATIONNAME")== null ? "" : _user.get("EDUCATIONNAME")));
                    jsonObject.put("TEL", _user.get("TEL"));
                    jsonObject.put("STATE", _user.get("STATE"));
                    jsonObject.put("SXBZ", (_user.get("SXBZ")== null ? "" : _user.get("SXBZ")));
                    jsonObject.put("MID", _user.get("MID"));

                    if (_user.get("OP1") == null || _user.get("OP1").equals("未公布")) {
                        jsonObject.put("STATEa", (_user.get("MCONTENT")== null ? "" : _user.get("MCONTENT")));
                        jsonObject.put("STATEb", "未公布");
                    } else {
                        jsonObject.put("LOGINTIME", (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString()));
                        jsonObject.put("STATEa", "最近登录："+ (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString())+"<br/>个人情况：" + (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")));
                        jsonObject.put("STATEb", "已公布");
                    }

                    jsonObject.put("FQSTATE", _user.get("FQSTATE"));
                    jsonObject.put("FQFID", _user.get("FQFID"));
                    jsonObject.put("NJNAME", _user.get("NJNAME"));
                    jsonObject.put("FID", _user.get("FID"));
                    if (_user.get("FQSTATE").equals("已放弃") && _user.get("FQFID").equals(flowid)) {
                        jsonObject.put("STATEc", "已放弃");
                    } else {
                        if (_user.get("NRID") != null) {
                            jsonObject.put("STATEc", "已推荐");
                        } else {
                            if (_user.get("STATE").equals("未处理")) {
                                jsonObject.put("STATEc", "未完成");
                            } else if (_user.get("STATE").equals("未通过")||_user.get("STATE").equals("不通过")||_user.get("STATE").equals("不合格")) {
                                jsonObject.put("STATEc", "已完成");
                            } else {
                                boolean bl = _user.get("FID").equals(flowid);
                                jsonObject.put("STATEc",  (bl == true ? "未完成" : "已完成"));
                            }
                        }
                    }

                    jsonArray.add(jsonObject);
                    num++;
                }
            }


            LayuiDataTableResultBean result = new LayuiDataTableResultBean();
            result.setCode(0);
            result.setMsg("success");
            result.setCount((int) allcount);
            result.setData(jsonArray);

            return result;
        } catch (Exception e) {
            e.printStackTrace();

            LayuiDataTableResultBean result = new LayuiDataTableResultBean();
            result.setCount(-1);
            result.setMsg("err");
            return result;
        }
    }


    //[start] 通过简历筛选
    @RequestMapping(value = "/ResumeOkAction", method = RequestMethod.POST)
    public void ResumeOkAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码

            BaseUser cuser = utilService.getSessionUser(request);
            if (cuser.getUdept().equals(deptcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    String[] ids = request.getParameterValues("id");
                    if (ids != null) {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> resumes = szService.getResumes(ids);
                        for (DUserResume dUserResume : resumes) {
                            if (dUserResume.getFid().equals(fid)
                                    && dUserResume.getFqstate().equals("未放弃")
                                    && dUserResume.getNrid() == null) {
                                dUserResume.setState("已通过");
                                dUserResume.setEditDate(new Date());
                                dUserResume.setEditUserid(cuser.getId());
                                dUserResume.setEditUserName(cuser.getUname());
                                editList.add(dUserResume);
                            }
                        }
                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的简历不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "请选择应聘者！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 批量通过简历筛选
    /**
     * 批量通过简历筛选
     * @param request
     * @param response
     */
    @RequestMapping(value = "/ResumeAllOkAction", method = RequestMethod.POST)
    public void ResumeAllOkAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码

            BaseUser cuser = utilService.getSessionUser(request);
            if (cuser.getUdept().equals(deptcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    //String[] ids = request.getParameterValues("ids");
                    String sid=request.getParameter("ids");
                    String[] ids=sid.split(",");
                    if(ids!=null&&ids.length>0){
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> resumes = szService.getResumes(ids);
                        for (DUserResume dUserResume : resumes) {
                            if (dUserResume.getFid().equals(fid)
                                    && dUserResume.getFqstate().equals("未放弃")
                                    && dUserResume.getNrid() == null) {
                                dUserResume.setState("已通过");
                                dUserResume.setEditDate(new Date());
                                dUserResume.setEditUserid(cuser.getId());
                                dUserResume.setEditUserName(cuser.getUname());
                                editList.add(dUserResume);
                            }
                        }
                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的简历不能执行此操作！", "no", response);
                        }

                    }else{
                        utilService.printJsonResult(false, "请至少选择应聘者！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]


    //[start] 未通过简历筛选
    @RequestMapping("/bz")
    public String BZ(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String id = request.getParameter("id").trim();
            model.addAttribute("id", id);
            model.addAttribute("pid", request.getParameter("pid").trim());
            model.addAttribute("fid", request.getParameter("fid").trim());
            model.addAttribute("deptcode", request.getParameter("deptcode").trim());
            DUserResume resume = szService.getResume(id);
            model.addAttribute("bz", resume.getSxbz());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Resume/bz";
    }

    @RequestMapping(value = "/ResumeNoAction", method = RequestMethod.POST)
    public void ResumeNoAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码

            BaseUser cuser = utilService.getSessionUser(request);
            if (cuser.getUdept().equals(deptcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    DUserResume dUserResume = szService.getResume(request.getParameter("id").trim());
                    if (dUserResume.getFid().equals(fid)
                            && dUserResume.getFqstate().equals("未放弃")
                            && dUserResume.getNrid() == null) {
                        dUserResume.setState("未通过");
                        dUserResume.setOp1("未公布");
                        dUserResume.setSxbz(request.getParameter("sxbz"));
                        dUserResume.setEditDate(new Date());
                        dUserResume.setEditUserid(cuser.getId());
                        dUserResume.setEditUserName(cuser.getUname());

                        utilService.editObject(dUserResume);
                        utilService.printJsonResult(true, "操作成功！", "no", response);
                    } else {
                        utilService.printJsonResult(false, "选中的简历不能执行此操作", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]


    //[start] 未通过简历筛选
    @RequestMapping(value = "/ResumeAllNoAction", method = RequestMethod.POST)
    public void ResumeAllNoAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            BaseUser cuser = utilService.getSessionUser(request);

            if (cuser.getUdept().equals(deptcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    String sid=request.getParameter("ids");
                    String[] ids=sid.split(",");
                    if(ids!=null&&ids.length>0){
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> resumes = szService.getResumes(ids);
                        String sxbz=request.getParameter("sxbz");
                        for (DUserResume dUserResume : resumes) {
                            if (dUserResume.getFid().equals(fid)
                                    && dUserResume.getFqstate().equals("未放弃")
                                    && dUserResume.getNrid() == null) {
                                dUserResume.setState("未通过");
                                dUserResume.setSxbz(sxbz);
                                dUserResume.setEditDate(new Date());
                                dUserResume.setEditUserid(cuser.getId());
                                dUserResume.setEditUserName(cuser.getUname());
                                editList.add(dUserResume);
                            }
                        }
                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的简历不能执行此操作！", "no", response);
                        }

                    }else {
                        utilService.printJsonResult(false, "请至少选择应聘者！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 流程扭转到下一步
    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        //List<DoBSUser> addusers = new ArrayList<DoBSUser>();
                        List<DUserResume> resumes = szService.getResumes(ids);
                        ProjectInfoFlow nFlow = szService.getNextFlow(pid, deptcode, fid);
                        for (DUserResume user : resumes) {
                            if (user.getFid().equals(fid)
                                    && user.getFqstate().equals("未放弃")
                                    && user.getNrid() == null) {
                                if(user.getState().equals("通过")||user.getState().equals("已通过")||user.getState().equals("已通过")){
                                    user.setFid(nFlow.getId());
                                    user.setPath(user.getPath() + nFlow.getId() + "/");
                                    editlist.add(user);

                                    /*DoBSUser userBS = new DoBSUser();
                                    userBS.setId(UUID.randomUUID().toString());
                                    userBS.setFid(nFlow.getId());
                                    userBS.setUserid(user.getId());
                                    userBS.setAddress("-");
                                    userBS.setAllnum(-1);
                                    userBS.setKcname("-");
                                    userBS.setNotice(EnumTools.NoticeState.未发通知.ordinal());
                                    userBS.setResult("-");
                                    userBS.setSeatnum(-1);
                                    userBS.setStartnum(-1);
                                    userBS.setStarttime("-");
                                    userBS.setTicketnum("-");
                                    //userBS.setAddDate(new Date());
                                    addusers.add(userBS);*/
                                }

                            }
                        }

                        if (editlist.size() > 0) {
                            nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                            editlist.add(nFlow);

                            utilService.editList(editlist);
                           // utilService.addList(addusers);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的简历不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }
    //[end]

    //[start] 查看简历

    //查看投递的简历
    @RequestMapping("/View")
    public String View(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            DUserResume resume = doResumeService.getDUserResume(id);
            if (resume == null) {
                model.addAttribute("valid", "no");
            } else {
                model.addAttribute("valid", "yes");
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("isornot", EnumTools.IsOrNotList());
                model.addAttribute("proname",resume.getProname());
                model.addAttribute("proid",resume.getProid());
                model.addAttribute("jname",resume.getJname());
                String rid = resume.getId();
                List<DUserEducation> educations = doResumeService.getUserEducation(rid);//教育背景
                List<DUserWorkExperience> workexperiences = doResumeService.getUserWorkExperience(rid); //工作经验
                List<DUserTraining> trainings = doResumeService.getUserTraining(rid); //培训经历
                List<DUserRewardPunishment> rewardpunishments = doResumeService.getUserRewardPunishment(rid); //获奖信息
                List<DUserFamily> families = doResumeService.getUserFamily(rid);//家庭成员

                model.addAttribute("userresume", resume);
                model.addAttribute("educations", educations);
                model.addAttribute("workexperiences", workexperiences);
                model.addAttribute("trainings", trainings);
                model.addAttribute("rewardpunishments", rewardpunishments);
                model.addAttribute("families", families);

                if (resume.getChangeDate() != null
                        && resume.getChangeDate().after(resume.getAddDate())) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    model.addAttribute("tips", "应聘者于：" + formatter.format(resume.getChangeDate()) + " 对简历进行了更新");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Resume/view";
    }

    //查看注册的简历
    @RequestMapping("/ViewResume")
    public String ViewResume(HttpServletRequest request, Model model) {
        try {
            String cardno = request.getParameter("cardno").trim();//身份证号
            UserResume resume = doResumeService.getUserResume(cardno);
            if (resume == null) {
                model.addAttribute("hasresume", "no");
            } else {
                model.addAttribute("hasresume", "yes");
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("isornot", EnumTools.IsOrNotList());

                String rid = resume.getId();
                List<UserEducation> educations = doResumeService.getUserEducation1(rid);//教育背景
                List<UserWorkExperience> workexperiences = doResumeService.getUserWorkExperience1(rid); //工作经验
                List<UserTraining> trainings = doResumeService.getUserTraining1(rid); //培训经历
                List<UserRewardPunishment> rewardpunishments = doResumeService.getUserRewardPunishment1(rid); //获奖信息
                List<UserFamily> families = doResumeService.getUserFamily1(rid);//家庭成员

                model.addAttribute("userresume", resume);
                model.addAttribute("educations", educations);
                model.addAttribute("workexperiences", workexperiences);
                model.addAttribute("trainings", trainings);
                model.addAttribute("rewardpunishments", rewardpunishments);
                model.addAttribute("families", families);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Resume/viewresume";
    }

    //[end]

    //[start] 修改简历

    @RequestMapping("/Edit")
    public String Edit(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            DUserResume resume = doResumeService.getDUserResume(id);
            if (resume == null) {
                model.addAttribute("valid", "no");
            } else {
                model.addAttribute("xb", utilService.optionList("A", "0"));
                model.addAttribute("mz", utilService.optionList("B", "0"));
                model.addAttribute("hkxz", utilService.optionList("C", "0"));
                model.addAttribute("hyzk", utilService.optionList("D", "0"));
                model.addAttribute("zzmm", utilService.optionList("E", "0"));
                model.addAttribute("xl", utilService.optionList("F", "0"));
                model.addAttribute("xw", utilService.optionList("G", "0"));
                model.addAttribute("byxs", utilService.optionList("H", "0"));
                model.addAttribute("posttype", utilService.optionList("I", "0"));
                model.addAttribute("jndj", utilService.optionList("O", "0"));
                model.addAttribute("qtzj", utilService.optionList("L", "0"));
                model.addAttribute("jg", utilService.optionListById("N", "0"));

                model.addAttribute("valid", "yes");
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("isornot", EnumTools.IsOrNotList());

                String rid = resume.getId();
                List<DUserEducation> educations = doResumeService.getUserEducation(rid);//教育背景
                List<DUserWorkExperience> workexperiences = doResumeService.getUserWorkExperience(rid); //工作经验
                List<DUserTraining> trainings = doResumeService.getUserTraining(rid); //培训经历
                List<DUserRewardPunishment> rewardpunishments = doResumeService.getUserRewardPunishment(rid); //获奖信息
                List<DUserFamily> families = doResumeService.getUserFamily(rid);//家庭成员

                model.addAttribute("userresume", resume);
                model.addAttribute("educations", educations);
                model.addAttribute("workexperiences", workexperiences);
                model.addAttribute("trainings", trainings);
                model.addAttribute("rewardpunishments", rewardpunishments);
                model.addAttribute("families", families);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/SZ/Resume/edit";
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/EditAction", method = RequestMethod.POST)
    public void EditAction(HttpServletRequest request, HttpServletResponse response, ResumeInfo resumeInfo) {
        try {
            List list = new ArrayList();

            //简历基本信息
            DUserResume resume = resumeInfo.getUserresume();
            String rid = resume.getId();
            DUserResume _resume = doResumeService.getDUserResume(rid);

            _resume.setSexcode(resume.getSexcode());
            _resume.setSexname(resume.getSexname());
            _resume.setMzcode(resume.getMzcode());
            _resume.setMzname(resume.getMzname());
            _resume.setHeight(resume.getHeight());
            _resume.setJg(resume.getJg());
            _resume.setZzmmcode(resume.getZzmmcode());
            _resume.setZzmmname(resume.getZzmmname());
            _resume.setMcode(resume.getMcode());
            _resume.setMname(resume.getMname());
            _resume.setHkcode(resume.getHkcode());
            _resume.setHkname(resume.getHkname());
            _resume.setHukouaddress(resume.getHukouaddress());
            _resume.setNowaddress(resume.getNowaddress());
            _resume.setHometel(resume.getHometel());
            _resume.setDistribution(resume.getDistribution());

            _resume.setWorkexperience(resume.getWorkexperience());
            _resume.setAttendancetime(resume.getAttendancetime());
            _resume.setPresentsalaryamount(resume.getPresentsalaryamount());
            _resume.setExpectedsalaryamount(resume.getExpectedsalaryamount());

            _resume.setPosttypecode(resume.getPosttypecode());
            _resume.setPosttypename(resume.getPosttypename());
            _resume.setPostspecialty(resume.getPostspecialty());
            _resume.setPostlevelcode(resume.getPostlevelcode());
            _resume.setPostlevelname(resume.getPostlevelname());
            _resume.setPostgettime(resume.getPostgettime());
            _resume.setPostcompany(resume.getPostcompany());

            _resume.setQllevelpid(resume.getQllevelpid());
            _resume.setQllevelcode(resume.getQllevelcode());
            _resume.setQllevelname(resume.getQllevelname());
            _resume.setQltype(resume.getQltype());
            _resume.setQlgettime(resume.getQlgettime());
            _resume.setQlorg(resume.getQlorg());

            _resume.setOthcode(resume.getOthcode());
            _resume.setOthdate(resume.getOthdate());
            _resume.setOthlevel(resume.getOthlevel());
            _resume.setOthname(resume.getOthname());
            _resume.setOthorg(resume.getOthorg());
            _resume.setOthtypecode(resume.getOthtypecode());
            _resume.setOthtypename(resume.getOthtypename());
            _resume.setOthzy(resume.getOthzy());

            //教育背景
            List<DUserEducation> educations = resumeInfo.getUsereducation();
            List<DUserEducation> xedus = new ArrayList<DUserEducation>();

            for (DUserEducation userEducation : educations) {
                //找出已经毕业的学历
                if (userEducation.getTcode().equals("0001")) {
                    xedus.add(userEducation);
                }

                userEducation.setId(UUID.randomUUID().toString());
                userEducation.setRid(rid);
                //证书类型
                userEducation.setZscode(userEducation.getTcode());
                userEducation.setZsname(userEducation.getTname() + "证书");
            }
            list.addAll(educations);

            //按照学历从高到低排序
            try {
                Collections.sort(xedus, new Comparator<DUserEducation>() {
                    @Override
                    public int compare(DUserEducation arg0, DUserEducation arg1) {
                        return arg0.getEducationcode().compareTo(arg1.getEducationcode());
                    }
                });
                if (xedus.size() > 0) {
                    _resume.setUniversity(xedus.get(0).getSchoolname());
                    _resume.setEducation(xedus.get(0).getEducationcode());
                    _resume.setEducationname(xedus.get(0).getEducationname());
                    _resume.setMajorname(xedus.get(0).getMajorname());
                } else {
                    utilService.printJsonResult(false, "至少填写一项已经毕业的学历信息", "no", response);
                    return;
                }
            } catch (Exception e) {
                utilService.printJsonResult(false, "至少填写一项已经毕业的学历信息", "no", response);
                return;
            }

            //工作经验
            List<DUserWorkExperience> workExperiences = resumeInfo.getUserworkexperience();
            for (DUserWorkExperience userWorkExperience : workExperiences) {
                if (userWorkExperience.getStarttime() != null
                        && userWorkExperience.getEndtime() != null
                        && userWorkExperience.getCompanyname().trim().length() > 0
                        && userWorkExperience.getPosition().trim().length() > 0
                        && userWorkExperience.getJobduties().trim().length() > 0) {
                    userWorkExperience.setId(UUID.randomUUID().toString());
                    userWorkExperience.setRid(rid);
                    list.add(userWorkExperience);
                }
            }

            //培训经历
            List<DUserTraining> trainings = resumeInfo.getUsertraining();
            for (DUserTraining userTraining : trainings) {
                if (userTraining.getStarttime() != null
                        && userTraining.getEndtime() != null
                        && userTraining.getCertificate().trim().length() > 0
                        && userTraining.getCourse().trim().length() > 0
                        && userTraining.getDescription().trim().length() > 0
                        && userTraining.getOrganization().trim().length() > 0) {
                    userTraining.setRid(rid);
                    userTraining.setId(UUID.randomUUID().toString());
                    list.add(userTraining);
                }
            }

            //获奖情况
            List<DUserRewardPunishment> rewardPunishments = resumeInfo.getUserrewardpunishment();
            for (DUserRewardPunishment userRewardPunishment : rewardPunishments) {
                if (userRewardPunishment.getEnterprise().trim().length() > 0
                        && userRewardPunishment.getGettime() != null
                        && userRewardPunishment.getName().trim().length() > 0
                        && userRewardPunishment.getReason().trim().length() > 0) {
                    userRewardPunishment.setRid(rid);
                    userRewardPunishment.setId(UUID.randomUUID().toString());
                    list.add(userRewardPunishment);
                }
            }

            //家庭成员
            List<DUserFamily> families = resumeInfo.getUserfamily();
            for (DUserFamily userFamily : families) {
                if (userFamily.getBirthday() != null
                        && userFamily.getContactway().trim().length() > 0
                        && userFamily.getName().trim().length() > 0
                        && userFamily.getProfession().trim().length() > 0
                        && userFamily.getRelationship().trim().length() > 0
                        && userFamily.getWorkunit().trim().length() > 0) {
                    userFamily.setRid(rid);
                    userFamily.setId(UUID.randomUUID().toString());
                    list.add(userFamily);
                }
            }

            List dlist = new ArrayList();
            dlist.addAll(doResumeService.getUserEducation(rid));//教育背景
            dlist.addAll(doResumeService.getUserWorkExperience(rid));//工作经验
            dlist.addAll(doResumeService.getUserTraining(rid));//培训经历
            dlist.addAll(doResumeService.getUserRewardPunishment(rid));//获奖情况
            dlist.addAll(doResumeService.getUserFamily(rid));//家庭成员

            utilService.editList(_resume, dlist, list);

            utilService.printJsonResult(true, "简历修改成功", "no", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }
    //[end]
}
